package com.sdy.sd;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class SdHint extends View {
    private static final String TAG = "SdHint";
    public String mRowHint = "-";
    public String mColHint = "-";
    public String mChuteHint = "-";
    public int mHintSt = ST_ALL_OFF;

    public static final int ST_ALL_OFF              = 0;
    public static final int ST_HINT_ON              = 1;
    public static final int ST_SHOW_WIN             = 2;

    private static final int mPandding = 8;
    private static float mHintTxtBaseLine = 0;
    Paint mPaint = null;
    float mHinTxtHeight = 0f;
    public float mPosWin = 0f;
    int WIN_FRM_CNT = 50;
    int mAniIdx = 0;
    int LEFT_OVERFLOW = 200;
    int RIGHT_OFFSET = 100;
    float mLastPos = 0f;
    Bitmap mBmp;
    String mWinTxt = "勝利！";

    public SdHint(Context context) {
        super(context);
        init_res();
    }
    public SdHint(Context context, AttributeSet attrs){
        super(context, attrs);
        init_res();
    }

    private float subGetTxtDisWid(String txt, float txtSize) {
        Paint paint= new Paint();
        paint.setTextSize(txtSize);

//        Rect rect = new Rect();
//        paint.getTextBounds(txt, 0, txt.length(), rect); //这种方法量出来的比 measureText及FontMetrics要小不少, 是否够用待确认TODO
//        Log.e(TAG, "paint.measureText(txt):" + paint.measureText(txt));
//        Log.e(TAG, "rect:" +  rect + ", .left" + rect.left + ", .right:" + rect.right + ", .top:" + rect.top + ", .bottom:" + rect.bottom);
        return paint.measureText(txt);
    }
    public void makeWinTxtBmp() {
        Paint txtPaint = new Paint();
        txtPaint.setTextSize(120 * FullscreenActivity.mDm.widthPixels / 1080);
        txtPaint.setColor(Color.WHITE);

        Paint.FontMetrics fm = txtPaint.getFontMetrics();
        fm = txtPaint.getFontMetrics();
        float winTxtDispHeight = fm.descent - fm.ascent;
        //Log.e(TAG, "winTxtDispHeight:" + winTxtDispHeight);
        float winTxtDispWid = subGetTxtDisWid(mWinTxt, 120 * FullscreenActivity.mDm.widthPixels / 1080);
        mBmp = Bitmap.createBitmap(Math.round(winTxtDispWid), Math.round(winTxtDispHeight), Bitmap.Config.ARGB_8888);
        Canvas tmpCanvas = new Canvas(mBmp);

        float baseLine = -1 * txtPaint.getFontMetrics().ascent;
        tmpCanvas.drawText(mWinTxt, 0, baseLine, txtPaint); //draw dst

        Paint mixPaint =  new Paint();
        mixPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

        int rnd = ParticleFW.rand(0, 10);
        Bitmap colorFull;
        if(rnd <= 6) colorFull = artPixView.makeBmp_linear_rgb((int)winTxtDispWid, (int)winTxtDispHeight);
        else colorFull = artPixView.makeBmp_linear_hsl((int)winTxtDispWid, (int)winTxtDispHeight);

        tmpCanvas.drawBitmap(colorFull, 0, 0, mixPaint); //draw src
    }

    private void init_res() {
        mPaint = new Paint();
        mPaint.setTextAlign(Paint.Align.LEFT);
        mPaint.setColor(Color.WHITE);
        mPaint.setTextSize(60 * FullscreenActivity.mDm.widthPixels / 1080);

        Paint.FontMetrics fm = mPaint.getFontMetrics();
        mHinTxtHeight = fm.descent - fm.ascent;
        mHintTxtBaseLine = -1 * fm.ascent;

        LEFT_OVERFLOW = 200 * FullscreenActivity.mDm.widthPixels / 1080;
        RIGHT_OFFSET = 100 * FullscreenActivity.mDm.widthPixels / 1080;

        //makeWinTxtBmp();
    }

    public void onDraw(Canvas canvas) {
        if(ST_HINT_ON == mHintSt) {
            canvas.drawText("→:" + mRowHint, mPandding, mPandding + mHintTxtBaseLine + mHinTxtHeight * 0, mPaint);
            canvas.drawText("↓:" + mColHint, mPandding, mPandding + mHintTxtBaseLine + mHinTxtHeight * 1, mPaint);
            canvas.drawText("□:" + mChuteHint, mPandding, mPandding + mHintTxtBaseLine + mHinTxtHeight * 2, mPaint);
            return;
        }
        if(ST_SHOW_WIN == mHintSt) {
            canvas.drawBitmap(mBmp, mPosWin, mPandding, null);
            updateAniPos();
            return;
        }
    }
    
    public SdHint hintReset() {
        mRowHint = "-";
        mColHint = "-";
        mChuteHint = "-";
        return this;
    }
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    public int startShowWin() {
        mHintSt = SdHint.ST_SHOW_WIN;
        mAniIdx = 0;
        updateAniPos();
        return 0;
    }

    public int updateAniPos() { //curIdx:[0, REDRAW_MAX]
        int step1 = WIN_FRM_CNT * 3 / 5;
        if(mAniIdx < step1) {
            double F5 = Math.pow(WIN_FRM_CNT - mAniIdx, 4); //左右反转,平移,幂加速
            mPosWin = (float)(F5 * (FullscreenActivity.mDm.widthPixels + LEFT_OVERFLOW) / Math.pow(WIN_FRM_CNT, 4) - LEFT_OVERFLOW); //缩放
            mLastPos = mPosWin;
            //Log.e(TAG, "curIdx:" + mAniIdx + ", mPosWin" + mPosWin + ", mLastPos" + mLastPos);
            invalidate();
            ++mAniIdx;
            return 1;
        }
        if(mAniIdx <= WIN_FRM_CNT) {
            double H36 = mAniIdx - step1 - 1; //校正
            double I36 = Math.pow(H36, 5);  //幂加速
            double L35 =  RIGHT_OFFSET - mLastPos; //向右移动距离
            double J36 = I36 * L35 / Math.pow(WIN_FRM_CNT - step1 - 1, 5); //缩放
            mPosWin = (float)(J36 + mLastPos); //平移
            invalidate();
            ++mAniIdx;
            return 1;
        }
        return 0;
    }
}
